package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Waybills;
import com.ruoyi.system.service.IWaybillsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.InventorysMapper;
import com.ruoyi.system.domain.Inventorys;
import com.ruoyi.system.service.IInventorysService;

/**
 * 库存Service业务层处理
 * 
 * @author he
 * @date 2025-09-17
 */
@Service
public class InventorysServiceImpl implements IInventorysService 
{
    @Autowired
    private InventorysMapper inventorysMapper;

    @Autowired
    private InventoryAdjustLogServiceImpl inventoryAdjustLogService;

    @Autowired
    private IWaybillsService waybillsService;

    /**
     * 查询库存
     * 
     * @param id 库存主键
     * @return 库存
     */
    @Override
    public Inventorys selectInventorysById(Long id)
    {
        return inventorysMapper.selectInventorysById(id);
    }

    /**
     * 查询库存列表
     * 
     * @param inventorys 库存
     * @return 库存
     */
    @Override
    public List<Inventorys> selectInventorysList(Inventorys inventorys)
    {
        return inventorysMapper.selectInventorysListNew(inventorys);
    }

    /**
     * 新增库存
     * 
     * @param inventorys 库存
     * @return 结果
     */
    @Override
    public int insertInventorys(Inventorys inventorys)
    {
        inventorys.setCreateTime(DateUtils.getNowDate());
    inventorys.setCreateUserId(SecurityUtils.getUserId());
    inventorys.setCreateUserName(SecurityUtils.getUsername());
        return inventorysMapper.insertInventorys(inventorys);
    }

    /**
     * 修改库存
     * 
     * @param inventorys 库存
     * @return 结果
     */
    @Override
    public int updateInventorys(Inventorys inventorys)
    {
        inventorys.setUpdateTime(DateUtils.getNowDate());

        return inventorysMapper.updateInventorys(inventorys);
    }

    /**
     * 批量删除库存
     * 
     * @param ids 需要删除的库存主键
     * @return 结果
     */
    @Override
    public int deleteInventorysByIds(Long[] ids)
    {
        return inventorysMapper.deleteInventorysByIds(ids);
    }

    /**
     * 删除库存信息
     * 
     * @param id 库存主键
     * @return 结果
     */
    @Override
    public int deleteInventorysById(Long id)
    {
        return inventorysMapper.deleteInventorysById(id);
    }

    @Override
    public int refreshInventory(Inventorys inventorys) {

        //重置当前库存 关联的所有库存日志
        int i = inventoryAdjustLogService.deleteInventoryAdjustLogByInventorId(inventorys.getId());
        //查找库存信息
        Inventorys inventorys1 = inventorysMapper.selectInventorysById(inventorys.getId());
        //更新 同仓库,同货物的运单  且是已完成,已审核 为未扣库存的
        String dictValue = DictUtils.getDictValue("waybill_state", "已完成");

        Waybills waybills = new Waybills();
        waybills.setStockId(inventorys1.getStockId());
        waybills.setItemId(inventorys1.getGoodsId());
        waybills.setCheckUp(Long.valueOf(1));
        waybills.setCheckUpTwo(Long.valueOf(2));
        waybills.setWaybillState(Long.valueOf(dictValue));
        waybillsService.updateWaybillsCheckUp(waybills);

        Waybills waybillSel = new Waybills();

        waybillSel.setWaybillState(Long.valueOf(dictValue));
        waybillSel.setCheckUp(Long.valueOf(1));

        //查找当前已完成数据
        List<Waybills> waybillsList = waybillsService.selectWaybillsComplate(waybills);

        //执行定时任务执行的 扣库存方法
        waybillsService.updateWaybillsToInventory(waybillsList);

        return 1;
    }
}
